package com.tesis_2.DA;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.tesis_2.BL.BLSeguridad;
import com.tesis_2.MO.MOReserva;

public class DAOReservas {
	
	///reservations/create_res/:rdate/:rtime/:qty/:table_id/:qtytable/:status
	
	public static MOReserva generateReserva(MOReserva Reserva, String lsPlatosSelected) throws ParseException, UnsupportedEncodingException{

		MOReserva objReserva = new MOReserva();
		
		// aqui va lo q corte
	    try{
	    	
	    	String url = DAOUtils.getServer()+"/reservations/create_res/"
					+Reserva.getFormatedRDate()+"/"+Reserva.getFormatedRTime()+
				"/"+Reserva.getQty().toString()+"/"+Reserva.getTable_id().toString()+"/"
					+Reserva.getQtytable().toString()+"/"+Reserva.getStatus().toString()+
					"/"+Reserva.getRestaurant_id()+"/"+  Reserva.getLocal_id() +
					"/" + BLSeguridad.getObjUusario().getId() + "/"+ lsPlatosSelected + ".json"; // toDO aqui debe ir el id del usuario
	    	
	    	JSONObject reserv = (new JSONReaderObject().execute(url).get());
	    	if (reserv == null) return objReserva;
	        reserv = reserv.getJSONObject("reservation");
	        
	        objReserva.setId(reserv.getInt("id"));
	        objReserva.setQty(reserv.getInt("qty"));
	        objReserva.setQtytable(reserv.getInt("qtytable"));
	        objReserva.setRdate(DAOUtils.parseDateYyyyMmDd(reserv.getString("rdate")));
	        objReserva.setRtime(DAOUtils.parseDateIso(reserv.getString("rtime")));
	        objReserva.setRestaurant_id(reserv.getInt("restaurant_id"));
	        objReserva.setLocal_id(reserv.getInt("local_id"));
	        objReserva.setUpdated_at(DAOUtils.parseDateIso(reserv.getString("updated_at")));
	        objReserva.setCreated_at(DAOUtils.parseDateIso(reserv.getString("created_at")));
	        
	        //{"reservation":{"created_at":"2012-12-13T22:03:01Z","id":5,"qty":1,"qtytable":1,
	        //"rdate":"2012-10-12","rtime":"2000-01-01T18:50:00Z","status":1,"table_id":1,
	        //"updated_at":"2012-12-13T22:03:01Z"}}
	        
	    }catch(JSONException e)        {
	    	Log.e("log_tag", "Error parsing data "+e.toString());
	    	return objReserva;
	    } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReserva;
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReserva;
		}
		return objReserva;

	}
	
	
	public static ArrayList<MOReserva> getReservasByUserId(Integer userid) throws ParseException, UnsupportedEncodingException{

		ArrayList<MOReserva> objReservas = new ArrayList<MOReserva>();

	    try{
	    	
	    	String url = DAOUtils.getServer()+ "/reservations/getResByUserId/"+userid +".json";
	    	
	    	JSONArray reserv = (new JSONReaderArray().execute(url).get());
	    	
	    	if (reserv == null) return objReservas;
	    	
	    	for (int i = 0; i < reserv.length(); i++) {
	    		
	    		JSONObject jsonrest = reserv.getJSONObject(i);
	    		jsonrest = jsonrest.getJSONObject("reservation");
	    		MOReserva objReserva = new MOReserva();
	    		objReserva.setId(jsonrest.getInt("id"));
	    		objReserva.setQty(jsonrest.getInt("qty"));
	    		objReserva.setQtytable(jsonrest.getInt("qtytable"));
	    		objReserva.setRdate(DAOUtils.parseDateYyyyMmDd(jsonrest.getString("rdate")));
	    		objReserva.setRtime(DAOUtils.parseDateIso(jsonrest.getString("rtime")));
	        	objReserva.setRestaurant_id(jsonrest.getInt("restaurant_id"));
	        	objReserva.setLocal_id(jsonrest.getInt("local_id"));
	        	objReserva.setRestaurant_name(jsonrest.getString("restaurant_name"));
	        	objReserva.setLocal_name(jsonrest.getString("local_name"));
	        	objReserva.setTable_id(jsonrest.getInt("table_id"));
	        	objReserva.setStatus(jsonrest.getInt("status"));
	        	objReserva.setUpdated_at(DAOUtils.parseDateIso(jsonrest.getString("updated_at")));
	        	objReserva.setCreated_at(DAOUtils.parseDateIso(jsonrest.getString("created_at")));
	        	
	        	objReservas.add(objReserva);
	        	
	    	}
	        
	        
	    }catch(JSONException e)        {
	    	Log.e("log_tag", "Error parsing data "+e.toString());
	    	return objReservas;
	    } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReservas;
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReservas;
		}
		return objReservas;

	}
	
	public static ArrayList<MOReserva> getReservasByRestLocal(Integer restid, Integer localid) throws ParseException, UnsupportedEncodingException{

		ArrayList<MOReserva> objReservas = new ArrayList<MOReserva>();

	    try{
	    	
	    	String url = DAOUtils.getServer()+ "/reservations/getResByRestLoc/"+restid +"/"+localid +".json";
	    	
	    	JSONArray reserv = (new JSONReaderArray().execute(url).get());
	    	
	    	if (reserv == null) return objReservas;
	    	
	    	for (int i = 0; i < reserv.length(); i++) {
	    		
	    		JSONObject jsonrest = reserv.getJSONObject(i);
	    		jsonrest = jsonrest.getJSONObject("reservation");
	    		MOReserva objReserva = new MOReserva();
	    		objReserva.setId(jsonrest.getInt("id"));
	    		objReserva.setQty(jsonrest.getInt("qty"));
	    		objReserva.setQtytable(jsonrest.getInt("qtytable"));
	    		objReserva.setRdate(DAOUtils.parseDateYyyyMmDd(jsonrest.getString("rdate")));
	    		objReserva.setRtime(DAOUtils.parseDateIso(jsonrest.getString("rtime")));
	        	objReserva.setRestaurant_id(jsonrest.getInt("restaurant_id"));
	        	objReserva.setLocal_id(jsonrest.getInt("local_id"));
	        	objReserva.setRestaurant_name(jsonrest.getString("restaurant_name"));
	        	objReserva.setLocal_name(jsonrest.getString("local_name"));
	        	objReserva.setTable_id(jsonrest.getInt("table_id"));
	        	objReserva.setStatus(jsonrest.getInt("status"));
	        	objReserva.setUpdated_at(DAOUtils.parseDateIso(jsonrest.getString("updated_at")));
	        	objReserva.setCreated_at(DAOUtils.parseDateIso(jsonrest.getString("created_at")));
	        	
	        	objReservas.add(objReserva);
	        	
	    	}
	        
	        
	    }catch(JSONException e)        {
	    	Log.e("log_tag", "Error parsing data "+e.toString());
	    	return objReservas;
	    } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReservas;
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return objReservas;
		}
		return objReservas;

	}
	
}
